package ni;

/*
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

import signals.FileProposal;
import signals.FileTransfer;
import signals.FileTransferNOK;
import signals.FileTransferOK;
import signals.Signal;
import signals.SignalTooBigException;
*/

/**
 * This class used to be used when we were doing the file transfer negociation in TCP
 * This class IS NOT USED ANYMORE but we did not have the heart to delete it
 * @author gb
 */
public class TCPReceiver extends Thread {
/*
    private Socket sock;
    private ChatNI chatNI;

    public TCPReceiver(Socket sock, ChatNI chatNI) {
        super();
        this.sock = sock;
        this.chatNI = chatNI;
    }

    public void run() {
        InputStream is = null;
        try {
            is = sock.getInputStream();
        } catch (IOException ex) {
            System.err.println("In TCPReceiver - run : could not get the input stream.");
        }
        OutputStream os = null;
        try {
            os = sock.getOutputStream();
        } catch (IOException ex) {
            System.err.println("In TCPReceiver - run : could not get the output stream.");
        }

        ChatNI.printDebug("TCPReceiver", "run", "Reading socket");

        //Reads Signal
        byte[] data = new byte[Signal.MAX_SIZE];
        try {
            is.read(data);
        } catch (IOException ex) {
            System.err.println("In TCPReceiver - run : could not read data from the input stream.");
        }
        Signal fileProposal = null;
        try {
            fileProposal = Signal.fromByteArray(data);
        } catch (IOException ex) {
            System.err.println("In TCPReceiver - run : could not read the signal from the data.");
        } catch (ClassNotFoundException ex) {
            System.err.println("In TCPReceiver - run : could not convert the input signal into a Signal.");
        }

        //Check if signal is a FileProposal
        if (fileProposal instanceof FileProposal) {
            ChatNI.printDebug("TCPReceiver", "run", "Received a file proposal");
            FileProposal fp = (FileProposal) fileProposal;

            //Ask user wether he wants the file or not
            FileTransfer ft = null;
            boolean ftAccepted = chatNI.processFileProposal(fp.getFileName(), fp.getSize(), fp.getFrom(), fp.getTo());

            try {
                if (ftAccepted) {
                    //Writing we want to receive the file                        
                    ChatNI.printDebug("TCPReceiver", "run", "Answering we want to receive the file");
                    os.write(Signal.toByteArray(new FileTransferOK()));
                } else {
                    ChatNI.printDebug("TCPReceiver", "run", "Answering we don't want to receive the file");
                    os.write(Signal.toByteArray(new FileTransferNOK()));
                }
            } catch (IOException ex) {
                System.err.println("In TCPReceiver - run : could not write the File Transfer Answer.");
            } catch (SignalTooBigException ex) {
                System.err.println("In TCPReceiver - run : signal size is too big.");
            }

            if (ftAccepted) {
                //Downloading the file
                if ((int) fp.getSize() != fp.getSize()) {
                    System.err.println("In TCPReceiver - run : file size is too big.");
                } else {
                    readingInputIntoFile(is, (int) fp.getSize(), fp.getFileName());
                }
            }

            chatNI.processFileReception(fp.getFileName(), fp.getFrom(), fp.getTo(), ftAccepted);

        } else {
            ChatNI.printDebug("TCPReceiver", "run", "Received an unexpected request");
        }

        try {
            sock.close();
        } catch (IOException ex) {
            System.err.println("In TCPReceiver - run : could not close the socket.");
        }
    }

    public static void readingInputIntoFile(InputStream is, int fileSize, String fileName) {
        FileOutputStream fos = null;
        BufferedOutputStream bos = null;
        ChatNI.printDebug("TCP_Receiver", "readingInputIntoFile", "Preparing to receive the file : " + fileName + " of " + fileSize + " bytes.");

        try {
            fos = new FileOutputStream("Download/" + fileName);
            bos = new BufferedOutputStream(fos);

        } catch (FileNotFoundException ex) {
            System.err.println("In TCP_Receiver - readingInputIntoFile : could not find the file. ");
        }

        int bytesRead;
        int count = 0;

        try {
            do {
                byte[] bytes = new byte[1000];
                bytesRead = is.read(bytes);
                count += bytesRead;
                ChatNI.printDebug("TCP_Receiver", "readingInputIntoFile", "Received " + bytesRead + " bytes.");
                fos.write(bytes, 0, bytesRead);
                ChatNI.printDebug("TCP_Receiver", "readingInputIntoFile", "Wrote " + bytesRead + " bytes.");
            } while (count < fileSize);
            ChatNI.printDebug("TCP_Receiver", "readingInputIntoFile", "Wrote " + count + " bytes in total.");
            fos.flush();
            fos.close();
        } catch (IOException ex) {
            System.err.println("In TCP_Receiver - readingInputIntoFile : could not read the b.i.s or write into the b.o.s. ");
        }
       
    }*/
}
